<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="droneName">
        <el-input
          v-model="queryParams.droneName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期范围" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          @change="handleDateChange"
        />
      </el-form-item>
      <el-form-item label="告警等级" prop="alarmLevel">
        <el-select v-model="queryParams.alarmLevel" placeholder="请选择告警等级" clearable style="width: 150px">
          <el-option
            v-for="dict in alarm_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="告警类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择告警类型" clearable style="width: 150px">
          <el-option
            v-for="dict in alarm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="所属组织" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入所属组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
       
      </el-form-item>
    </el-form>
<!-- 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['healthy:equipmentalarm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="alarmList">
      <el-table-column label="告警码" align="center" prop="alarmCode" />
      <el-table-column label="设备名称" align="center" prop="droneName" />
      <el-table-column label="告警等级" align="center" prop="alarmLevel">
        <template #default="{ row }">
          <dict-tag v-if="row?.alarmLevel" :options="alarm_level" :value="row.alarmLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="告警类型" align="center" prop="alarmType">
        <template #default="{ row }">
          <dict-tag v-if="row?.alarmType" :options="alarm_type" :value="row.alarmType"/>
        </template>
      </el-table-column>
      <el-table-column label="所属组织" align="center" prop="deptName" />
      <el-table-column label="告警内容" align="center" prop="errorDetail" :show-overflow-tooltip="true" />
      <el-table-column label="告警时间" align="center" prop="triggerTime" width="180">
        <template #default="{ row }">
          <span>{{ row.triggerTime ? parseTime(row.triggerTime) : '' }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { listEquipmentAlarm, exportEquipmentAlarm } from "@/api/healthy/equipmentalarm";
import { useDict } from "@/utils/dict.js";

// 使用字典
const { alarm_level, alarm_type } = useDict(
  'alarm_level',
  'alarm_type'
);

// 定义数据（相当于 data()）
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const alarmList = ref([]);
const dateRange = ref([]);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  alarmCode: null,
  droneName: null,
  alarmLevel: null,
  alarmType: null,
  deptName: null,
  errorDetail: null,
  beginTime: null,
  endTime: null,
  status: "0",
});

// 获取告警列表
const getList = async () => {
  loading.value = true;
  try {
    const response = await listEquipmentAlarm(queryParams);
    if (response?.rows) {
      alarmList.value = response.rows;
      total.value = response.total || 0;
    } else {
      alarmList.value = [];
      total.value = 0;
    }
  } catch (error) {
    console.error('获取告警列表失败:', error);
    alarmList.value = [];
    total.value = 0;
  } finally {
    loading.value = false;
  }
};

// 日期范围变更处理
const handleDateChange = (val) => {
  if (val) {
    queryParams.beginTime = val[0];
    queryParams.endTime = val[1];
  } else {
    queryParams.beginTime = null;
    queryParams.endTime = null;
  }
};

// 搜索操作
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

// 重置搜索
const resetQuery = () => {
  dateRange.value = [];
  Object.assign(queryParams, {
    pageNum: 1,
    pageSize: 10,
    alarmCode: null,
    droneName: null,
    alarmLevel: null,
    alarmType: null,
    deptName: null,
    errorDetail: null,
    beginTime: null,
    endTime: null,
    status: "0",
  });
  handleQuery();
};

// 导出数据
const handleExport = () => {
  exportEquipmentAlarm(queryParams);
};

// 在组件挂载时自动调用 getList
onMounted(() => {
  getList();
});
</script>

<style>
.mb8 {
  margin-bottom: 8px;
}
</style>